import { request } from './index'
import type { Task } from '@/types'

export const taskApi = {
  // 获取所有任务
  getAll(): Promise<Task[]> {
    return request.get('/tasks')
  },

  // 根据ID获取任务
  getById(id: number): Promise<Task> {
    return request.get(`/tasks/${id}`)
  },

  // 创建任务
  create(data: Omit<Task, 'id' | 'createdAt' | 'updatedAt'>): Promise<Task> {
    return request.post('/tasks', data)
  },

  // 更新任务
  update(id: number, data: Partial<Task>): Promise<Task> {
    return request.put(`/tasks/${id}`, data)
  },

  // 删除任务
  delete(id: number): Promise<void> {
    return request.delete(`/tasks/${id}`)
  },

  // 根据项目ID获取任务
  getByProject(projectId: number): Promise<Task[]> {
    return request.get(`/tasks/project/${projectId}`)
  },

  // 获取分配给我的任务
  getMyAssigned(): Promise<Task[]> {
    return request.get('/tasks/assigned-to-me')
  },

  // 获取我创建的任务
  getMyCreated(): Promise<Task[]> {
    return request.get('/tasks/created-by-me')
  },

  // 根据状态获取任务
  getByStatus(status: string): Promise<Task[]> {
    return request.get(`/tasks/status/${status}`)
  },

  // 更新任务状态
  updateStatus(id: number, status: string): Promise<Task> {
    return request.patch(`/tasks/${id}/status`, { status })
  },

  // 分配任务
  assign(id: number, assigneeId: number): Promise<Task> {
    return request.patch(`/tasks/${id}/assign`, { assigneeId })
  },

  // 搜索任务
  search(keyword: string): Promise<Task[]> {
    return request.get(`/tasks/search?keyword=${keyword}`)
  },

  // 根据优先级获取任务
  getByPriority(priority: string): Promise<Task[]> {
    return request.get(`/tasks/priority/${priority}`)
  },

  // 获取任务评论
  getComments(id: number): Promise<any[]> {
    return request.get(`/tasks/${id}/comments`)
  },

  // 添加任务评论
  addComment(id: number, content: string): Promise<any> {
    return request.post(`/tasks/${id}/comments`, { content })
  },

  // 上传任务附件
  uploadAttachment(id: number, file: File): Promise<{ url: string }> {
    const formData = new FormData()
    formData.append('file', file)
    return request.post(`/tasks/${id}/attachments`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}